<template>
	<div class="system-user-container">
		<el-card shadow="hover">
			<el-table :data="state.tableData.data" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="userName" label="邮件类型" show-overflow-tooltip align="center"></el-table-column>
				<el-table-column prop="userNickname" label="邮件主体" show-overflow-tooltip align="center"></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button text type="primary" @click="onOpenEditUser(scope.row)">编辑</el-button>
						<el-button size="small" text type="danger" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
	</div>
</template>

<script setup name="systemUser">
import { ElMessageBox, ElMessage } from 'element-plus';

const addUserRef = ref();
const editUserRef = ref();
const state = reactive({
	name: '',
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			pageNum: 1,
			pageSize: 10,
		},
	},
});
// 初始化表格数据
const initTableData = () => {
	const data = [];
	for (let i = 0; i < 2; i++) {
		data.push({
			userName: i === 0 ? 'admin' : 'test',
			userNickname: i === 0 ? '我是管理员' : '我是普通用户',
			roleSign: i === 0 ? 'admin' : 'common',
			department: i === 0 ? ['admin', 'IT外包服务'] : ['admin', '资本控股'],
			phone: '12345678910',
			email: 'admin@123.com',
			sex: '女',
			password: '123456',
			overdueTime: new Date(),
			status: true,
			describe: i === 0 ? '不可删除' : '测试用户',
			createTime: new Date().toLocaleString(),
		});
	}
	state.tableData.data = data;
	state.tableData.total = state.tableData.data.length;
};
// 打开新增用户弹窗
const onOpenAddUser = () => {
	addUserRef.value.openDialog();
};
// 打开修改用户弹窗
const onOpenEditUser = (row) => {
	editUserRef.value.openDialog(row);
};
// 删除用户
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除账户名称：“${row.userName}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			ElMessage.success('删除成功');
		})
		.catch(() => {});
};
// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.pageSize = val;
};
// 分页改变
const onHandleCurrentChange = (val) => {
	state.tableData.param.pageNum = val;
};
// 页面加载时
onMounted(() => {
	initTableData();
});
</script>
